<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.HashMap"%>
<%@page import="redis.clients.jedis.Jedis"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%!public final static String HOST = "HOST";
	public final static String PORT = "PORT";
	public final static String NAME = "NAME";
	public final static String PASSWORD = "PASSWORD";
	public final static String STATUS = "STATUS";
	public final static String STATUS_RUN = "RUN";
	public final static String STATUS_STOP = "STOP";
	public int selectServer = 0;
	public int s = -1;
	public List<HashMap<String, String>> servers = new ArrayList<HashMap<String, String>>();
	public HashMap<String, String> actServers = null;
	public Jedis jedis = null;%>
<%
	servers.clear();
	s = Integer.parseInt(isNull(request.getParameter("s"))?"-1":request.getParameter("s"));
	HashMap<String, String> server1 = new HashMap<String, String>();
	server1.put(NAME, "OSCHINA");
	server1.put(HOST, "10.4.7.20");
	server1.put(PORT, "5011");
	server1.put(PASSWORD, "b3e9cc97-0091-45e4-bddf-6730313244a2");
	servers.add(server1);
	
	// 建立连接
	buildConnect();
%>
<%!public void buildConnect() {
		if(jedis != null && jedis.isConnected()) {
			jedis.disconnect();
			jedis=null;
		}
		int index = 0;
		for (HashMap<String, String> server : servers) {
			if (index == s) {
				selectServer = s;
				server.put(STATUS, STATUS_RUN);
				jedis = new Jedis(server.get(HOST), Integer.parseInt(server.get(PORT)));
				if(server.get(PASSWORD) != null ) {
					jedis.auth(server.get(PASSWORD));
				}
				try{
					if("PONG".equals(jedis.ping())) {
						actServers = server;
					} else {
						jedis = null;
					}
				}catch(Exception e) {
					jedis = null;
					actServers = null;
				}
			} else {
				server.put(STATUS, STATUS_STOP);
			}
			index++;
		}
	}
	%>
	<%!
	public long countKey(String keyType, Jedis redis, String key){
		long size = 0;
		if("string".equals(keyType)) {
			size = redis.get(key).length();
		} else if("hash".equals(keyType)) {
			size = redis.hlen(key);
		} else if("list".equals(keyType)) {
			size = redis.llen(key);
		} else if("zset".equals(keyType)) {
			size = redis.zrange(key, 0, -1).size();
		} else if("set".equals(keyType)) {
			size = redis.smembers(key).size();
		}
		return size;
	}
	%>
	<%!
	public HashMap<String, String> parseInfo(String info){
		HashMap<String, String> hashMap = new HashMap<String, String>();
		String[] ops = null;
		String[] opsTmp = null;
		String tmp = "";
		for (String c : info.split("\r\n")) {
			tmp = "";
			ops = c.split(":");
			int len = ops.length;
			if(len >= 2) {
				for(int i=1;i<len;i++) {
					tmp +=ops[i];
				}
				hashMap.put(ops[0], tmp);
			}
		}
		return hashMap;
	}
	
	public boolean isNull(String c){
		boolean flag = false;
		if(c == null || "null".equals(c) || "".equals(c)){
			flag = true;
		}
		return flag;
	}
	
	public String parseNull(Object o) {
		return o == null ? "" : o.toString();
	}
	
// 	public String laiyigejiema(String soga) throws UnsupportedEncodingException{
// 		if(soga == null) {return null;}
// 		return URLDecoder.decode(soga, "utf-8");
// 	}
// 	public String unlaiyigejiema(String soga) throws UnsupportedEncodingException{
// 		if(soga == null) {return null;}
// 		return URLDecoder.decode(soga, "utf-8");
// 	}
	
	public String unhaiyaoyige(String nane) throws UnsupportedEncodingException{
		if(nane == null) return "";
		return new String(nane.getBytes("UTF-8"),"ISO-8859-1");
	}
	public String haiyaoyige(String nane) throws UnsupportedEncodingException{
		if(nane == null) return "";
		return new String(nane.getBytes("ISO-8859-1"),"UTF-8");
	}
	%>
